/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月15日
 * @version 0.1
 */
package com.suredy.resource.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.Tree;
import com.suredy.resource.model.Segment;
import com.suredy.resource.service.MenuSrv;
import com.suredy.resource.service.SegmentSrv;
import com.suredy.security.entity.Role2PermissionEntity;
import com.suredy.security.entity.User2PermissionEntity;
import com.suredy.security.service.PermissionSrv;
import com.suredy.security.service.Role2PermissionSrv;
import com.suredy.security.service.User2PermissionSrv;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value="/config")
public class ResourceCtrl extends BaseCtrl{
	
	@Autowired
	private MenuSrv menuSrv;
	
	@Autowired
	private SegmentSrv segmentSrv;
	
	@Autowired
	private Role2PermissionSrv role2permissionSrv;
	
	@Autowired
	private User2PermissionSrv user2permissionSrv;
	
	@Autowired
	private PermissionSrv permissionSrv;
	
	@RequestMapping("resource/user")
	@ResponseBody
	public Object ResourceUser(String userId) {
		Tree root = new Tree();
		root.setText("安全资源");
		root.setCollapse(false);
		root.setChildren(new ArrayList<Tree>());
		
		List<User2PermissionEntity> userPermissions = this.user2permissionSrv.getByUser(userId);
		List<String> resources = new ArrayList<String>();
		for (User2PermissionEntity u2p : userPermissions) {
			resources.add(u2p.getPermission().getResource().getId());
		}
		Tree menuTree = new Tree();
		menuTree.setText("菜单类");
		menuTree.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null, resources);
		if (menuChildren != null) {
			menuTree.setChildren(new ArrayList<Tree>());
			menuTree.getChildren().addAll(menuChildren);
		}
		
		Tree segments = new Tree();
		List<Tree> segmentTree = new ArrayList<Tree>();
		List<Segment> segmentsData = this.segmentSrv.getAll();
		if (segmentsData != null) {
			for (Segment segment : segmentsData) {
				Tree temp = new Tree();
				temp.setText(segment.getName());
				temp.setData(segment);
				if (resources.contains(segment.getResourceId())) {
					temp.setChecked(true);
				}
				segmentTree.add(temp);
			}
		}
		segments.setText("单点控制");
		segments.setCollapse(true);
		segments.setChildren(new ArrayList<Tree>());
		segments.getChildren().addAll(segmentTree);
		
		root.getChildren().add(menuTree);
		root.getChildren().add(segments);
		return root;

	}
	
	@RequestMapping("resource/role")
	@ResponseBody
	public Object ResourceRole(String roleId) {
		Tree root = new Tree();
		root.setText("安全资源");
		root.setCollapse(false);
		root.setChildren(new ArrayList<Tree>());
		List<Role2PermissionEntity> rolePermissions = this.role2permissionSrv.getByRole(roleId);
		List<String> resources = new ArrayList<String>();
		for (Role2PermissionEntity r2p : rolePermissions) {
			resources.add(r2p.getPermission().getResource().getId());
		}
		Tree menuTree = new Tree();
		menuTree.setText("菜单类");
		menuTree.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null, resources);
		if (menuChildren != null) {
			menuTree.setChildren(new ArrayList<Tree>());
			menuTree.getChildren().addAll(menuChildren);
		}
		
		Tree segments = new Tree();
		List<Tree> segmentTree = new ArrayList<Tree>();
		List<Segment> segmentsData = this.segmentSrv.getAll();
		if (segmentsData != null) {
			for (Segment segment : segmentsData) {
				Tree temp = new Tree();
				temp.setText(segment.getName());
				temp.setData(segment);
				if (resources.contains(segment.getResourceId())) {
					temp.setChecked(true);
				}
				segmentTree.add(temp);
			}
		}
		segments.setText("单点控制");
		segments.setCollapse(true);
		segments.setChildren(new ArrayList<Tree>());
		segments.getChildren().addAll(segmentTree);
		
		root.getChildren().add(menuTree);
		root.getChildren().add(segments);
		return root;

	}

	@RequestMapping("resource/tree")
	@ResponseBody
	public Object ResourceTree() {
		Tree root = new Tree();
		root.setText("安全资源");
		root.setCollapse(false);
		root.setChildren(new ArrayList<Tree>());
		
		Tree menus = new Tree();
		menus.setText("菜单类");
		menus.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null);
		if (menuChildren != null) {
			menus.setChildren(new ArrayList<Tree>());
			menus.getChildren().addAll(menuChildren);
		}
		
		Tree segments = new Tree();
		List<Tree> segmentTree = new ArrayList<Tree>();
		List<Segment> segmentsData = this.segmentSrv.getAll();
		if (segmentsData != null) {
			for (Segment segment : segmentsData) {
				Tree temp = new Tree();
				temp.setText(segment.getName());
				temp.setData(segment);
				segmentTree.add(temp);
			}
		}
		segments.setText("单点控制");
		segments.setCollapse(true);
		segments.setChildren(new ArrayList<Tree>());
		segments.getChildren().addAll(segmentTree);
		
		
		root.getChildren().add(menus);
		root.getChildren().add(segments);
		return root;

	}
	
	
	@RequestMapping(value = "resource/view")
	public ModelAndView MenuManager() {
		ModelAndView view = new ModelAndView("/config/resource/resource-view");
		return view;
	}
	
}
